﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.ClientService.Dto;
using WMS.Common;
using WMS.TakenOffs;
using WMS.TakenoffService;
using WMS.TakenoffService.Dto;

namespace WMS.TakenoffServicess
{
    public class TakenoffServices:ApplicationService,ITakenoffService
    {
        private readonly IRepository<Takenoff,int> repository;

        public TakenoffServices(IRepository<Takenoff,int> repository)
        {
            this.repository = repository;
        }

        /// <summary>
        /// 获取下架数据
        /// </summary>
        /// <param name="productCode"></param>
        /// <param name="productName"></param>
        /// <param name="index"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<PagedResult> GetTakensdata(string? productCode, string? productName, int index = 1, int limit = 10)
        {
            var predicate = PredicateBuilder.New<Takenoff>(true);
            if (!string.IsNullOrEmpty(productCode))
            {
                predicate = predicate.And(m => m.ProductCode.Contains(productCode));
            }
            if (!string.IsNullOrEmpty(productName))
            {
                predicate = predicate.And(m => m.ProductName.Contains(productName));
            }
            var takenslist = repository.GetListAsync(predicate).Result;
            PagedResult result = new PagedResult();
            result.CurrentPage = index;
            result.PageSize = limit;
            result.RowCount = takenslist.Count();
            result.Queryable = takenslist.OrderBy(x => x.Id).Skip((index - 1) * limit).AsQueryable();
            result.Queryable = result.Queryable.MapToList<SearchTakendto>().AsQueryable();
            return result;
        }

        /// <summary>
        /// 删除下架信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteTakens(int id)
        {
            await repository.DeleteAsync(id);
            return 1;
        }

        /// <summary>
        /// 添加客户信息
        /// </summary>
        /// <param name="addTakenoffdto"></param>
        /// <returns></returns>
        public async Task<int> AddTakenoffs(AddTakenoffdto addTakenoffdto)
        {
            var takenmodel = addTakenoffdto.MapTo<Takenoff>();
            await repository.InsertAsync(takenmodel);
            return 1;
        }

        /// <summary>
        /// 通过id获取下架信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<SearchTakendto> SelectByidTaken(int id)
        {
            var entity = repository.GetAsync(id).Result;
            var takendto = entity.MapTo<SearchTakendto>();
            return takendto;
        }

        /// <summary>
        /// 修改下架信息
        /// </summary>
        /// <param name="searchTakendto"></param>
        /// <returns></returns>
        public async Task<int> UpdateTakens(SearchTakendto searchTakendto)
        {
            var entity = searchTakendto.MapTo<Takenoff>();
            await repository.UpdateAsync(entity);
            return 1;
        }
    }
}
